package com.jeasonzhao.commons.parser.expression;

public abstract class SyntaxNode
{
    public static interface INameConvertor
    {
        public String convert(String original);
    }

    public SyntaxNode()
    {
    }

    public String toString()
    {
        return getExpression();
    }

    public abstract String getExpression();

    public abstract ValuePair eval(IEvalProvider provider)
        throws EvalException;

    public void replaceVariableName(String ...map)
    {
        if(null != map)
        {
            java.util.Hashtable<String,String> m = new java.util.Hashtable<String,String>();
            for(int n = 0;n < map.length && (n + 1) < map.length;n++)
            {
                String key = map[n];
                String value = map[n + 1];
                if(key != null && value != null)
                {
                    m.put(key,value);
                }
            }
            this.replaceVariableName(m);
        }
    }

    public abstract void replaceVariableName(INameConvertor map);

    public void replaceVariableName(final java.util.Map<String,String> map)
    {
        if(map == null || map.size() < 1)
        {
            return;
        }
        this.replaceVariableName(new INameConvertor()
        {
            public String convert(String original)
            {
                if(null != original)
                {
                    for(java.util.Map.Entry<String,String> e : map.entrySet())
                    {
                        if(original.equalsIgnoreCase(e.getKey()))
                        {
                            return e.getValue();
                        }
                    }
                    return original;
                }
                else
                {
                    return original;
                }
            }
        });
    }
}
